/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.checker;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author kevin
 */
public class ObjectType implements RefType {

	private static final PermKind[] NO_DIMS = new PermKind[0];

	private static final Map<PermKind, ObjectType> borrowedInstances =
		new TreeMap<PermKind, ObjectType>();
	private static final Map<PermKind, ObjectType> capturedInstances =
		new TreeMap<PermKind, ObjectType>();
	private static final Map<PermKind, ObjectType> consumedInstances =
		new TreeMap<PermKind, ObjectType>();

	private final PermKind perm;
	private final boolean borrowed;
	private final boolean released;

	public static RefType getInstance(PermKind perm,
			boolean borrowed, boolean released, PermKind... dims) {
		if (perm == null)
			return null;
		Map<PermKind, ObjectType> m;
		if (released) {
			m = capturedInstances;
		} else {
			m = borrowed ? borrowedInstances : consumedInstances;
		}
		ObjectType result = m.get(perm);
		if (result == null) {
			result = new ObjectType(perm, borrowed, released);
			m.put(perm, result);
		}
		if (dims.length == 0)
			return result;
		else
			return new ArrayType(result, dims);
	}

	private ObjectType(PermKind perm, boolean borrowed, boolean released) {
		if (released)
			// force borrowed internally to be false
			borrowed = false;
		this.perm = perm;
		this.borrowed = borrowed;
		this.released = released;
	}

	public PermKind getPerm() {
		return perm;
	}

	/**
	 * {@code false} if {@link #isReleased()} even though the source annotations
	 * allow both {@code borrowed} and {@code released} to be {@code true}.
	 * @return
	 */
	public boolean isBorrowed() {
		return borrowed;
	}

	/**
	 * {@code false} if {@link #isBorrowed()} even though the source annotations
	 * allow both {@code borrowed} and {@code released} to be {@code true}.
	 * @return
	 */
	public boolean isReleased() {
		return released;
	}

	/**
	 * Neither {@link #isBorrowed()} nor {@link #isReleased()}
	 * @return
	 */
	public boolean isConsumed() {
		return ! borrowed && ! released;
	}

	public boolean isStrongerThan(RefType other) {
		if (other == null)
			return true;
		if (! (other instanceof ObjectType))
			// incomparable
			return false;
		ObjectType o = (ObjectType) other;
		return perm.isStrongerThan(o.perm);
	}

	public RefType join(RefType other) {
		if (other == null)
			return null;
		if (! (other instanceof ObjectType))
			// incomparable
			throw new IllegalArgumentException("Not an object: " + other);
		ObjectType o = (ObjectType) other;
		return getInstance(perm.join(o.perm), borrowed, released);
	}

	public RefType leak(PermKind p) {
		// borrowed should be true when leaking excl perms anyway but just in case
		return getInstance(p.weaken(perm), ! perm.isSymmetric() || borrowed, released);
	}

	@Override
	public PermKind[] getElemDims() {
		return NO_DIMS;
	}

	@Override
	public String toString() {
		return perm + "[" + borrowed + "," + released + "]";
	}
}
